package dao;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import beans.CoresBeans;
import conexao.Conexao;

public class CoresDao {
	public List<CoresBeans> Listar(CoresBeans cor) {
		List<CoresBeans> listaCores = null;
		
		try {
			
			CallableStatement cstmt = Conexao.getInstance().prepareCall("{call dbo.ListarCores(?)}");
		     if(1 != 0) cstmt.setInt(1, cor.getCod_Fio()); 
		      ResultSet rs = cstmt.executeQuery();
		      Conexao.getInstance().commit();
			
			listaCores = new ArrayList<CoresBeans>();
			while (rs.next()) {
				CoresBeans cor2 = new CoresBeans();
				cor2.setCod_Cor(rs.getInt("Cod_Cor"));
				cor2.setNom_Cor(rs.getString("Nom_Cor"));
				cor2.setCod_UsuaCad(rs.getInt("Cod_UsuaCad"));
				cor2.setNom_UsuaCad(rs.getString("Nom_UsuaCad"));
				cor2.setDat_Cad(rs.getDate("Dat_Cad"));
				listaCores.add(cor2);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return listaCores;
	}	
	
	public int Inserir(CoresBeans cor) { 
		int ret = -1;
		
		try {
			CallableStatement cstmt = Conexao.getInstance().prepareCall("{call dbo.InserirCor(?, ?, ?)}");
		      cstmt.setString(1, cor.getNom_Cor());
		      cstmt.setInt(2, cor.getCod_UsuaCad());    
		      cstmt.registerOutParameter(3, java.sql.Types.INTEGER);
		      cstmt.execute();
		      ret = cstmt.getInt(3);
		      Conexao.getInstance().commit();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return ret;
	}
	
	public int Excluir(CoresBeans cor) {
		int ret = -1;
		
		try {
			CallableStatement cstmt = Conexao.getInstance().prepareCall("{call dbo.ExcluirCor(?, ?)}");
		      cstmt.setInt(1, cor.getCod_Cor());   
		      cstmt.registerOutParameter(2, java.sql.Types.INTEGER);
		      cstmt.execute();
		      ret = cstmt.getInt(2);
		      Conexao.getInstance().commit();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return ret;
	}
}
